package com.qf.flashlib.base;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.qf.flashlib.R;
import com.qf.flashlib.app.BaseApp;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.util.ScreenUtils;
import com.qf.flashlib.widget.CustomProgressDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 2018/10/31 14:09
 */
public abstract class BaseActivity extends BaseEmptyActivity implements IView {

    private Unbinder unbinder;
    private List<BasePresenter> mPresenters = new ArrayList<>();
    private CustomProgressDialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenUtils.setCustomDensity(this, BaseApp.getInstance());
        super.onCreate(savedInstanceState);
        int layoutId = getLayoutId();
        if (layoutId == 0)
            return;
        setContentView(layoutId);
        unbinder = ButterKnife.bind(this);
        initInject();
        if (useEvent()) {
            EventBus.getDefault().register(this);
        }

        initViewAndData();


    }

    protected boolean useEvent() {
        return false;
    }
    protected abstract int getLayoutId();

    protected abstract void initViewAndData();


    protected void initInject() {

    }

    protected<T>  void  postEvent(T event) {
        EventBus.getDefault().post(event);
    }

    protected void initPresenters(BasePresenter... presenter) {

        if (presenter.length > 0) {
            Collections.addAll(mPresenters, presenter);
        }
        for (BasePresenter mPresenter : mPresenters) {
            mPresenter.attachView(this);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        for (BasePresenter basePresenter : mPresenters) {
            basePresenter.detachView();
        }
        if (useEvent()) {
            EventBus.getDefault().unregister(this);
        }
    }

    /* 展示加载中对话框 */
    public void showLoadingDialog(String tip, boolean cancelable)
    {
        if (mLoadingDialog == null) {
            mLoadingDialog = new CustomProgressDialog(this);
            mLoadingDialog.setCanceledOnTouchOutside(false);
            mLoadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
            {
                @Override
                public void onCancel(DialogInterface dialog)
                {
                }
            });
        }
        mLoadingDialog.setTipText(tip);
        mLoadingDialog.setCancelable(cancelable);
        if (!mLoadingDialog.isShowing())
            mLoadingDialog.show();

    }

    /* 展示加载中对话框 */
    public void showLoadingDialog()
    {
        this.showLoadingDialog("请稍候...", true);
    }

    /* 关闭对话框 */
    public void dismissLoadingDialog()
    {
        if (mLoadingDialog != null && mLoadingDialog.isShowing())
            mLoadingDialog.dismiss();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void hideLoading() {
        dismissLoadingDialog();
    }

    @Override
    public void onError(ApiException e) {

    }

    @Override
    public void showMessage(String msg) {
        FlashLib.showToast(msg);
    }

    public int getFragmentGroupId() {
        return 0;
    }


}
